package com.tanqing.ddd.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.FastAutoGenerator;
import com.baomidou.mybatisplus.generator.IFill;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.TemplateType;
import com.baomidou.mybatisplus.generator.config.po.TableField;
import com.baomidou.mybatisplus.generator.config.rules.DateType;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.baomidou.mybatisplus.generator.fill.Column;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.jdbc.ScriptRunner;

public class MyBaseBatisPlusGen {
    public MyBaseBatisPlusGen() {
    }

    public static void before(DataSourceConfig.Builder DATA_SOURCE_CONFIG) throws SQLException {
        Connection conn = DATA_SOURCE_CONFIG.build().getConn();
        ScriptRunner scriptRunner = new ScriptRunner(conn);
        scriptRunner.setAutoCommit(true);
        conn.close();
    }

    public static void gen(ArrayList<String> tableList, String parentPackageName, String author, DataSourceConfig.Builder DATA_SOURCE_CONFIG) {
        gen(tableList, parentPackageName, author, DATA_SOURCE_CONFIG, false, "./src/main/java");
    }

    public static void gen(ArrayList<String> tableList, String parentPackageName, String author, DataSourceConfig.Builder DATA_SOURCE_CONFIG, String outPutdDir) {
        gen(tableList, parentPackageName, author, DATA_SOURCE_CONFIG, false, outPutdDir);
    }

    public static void gen(ArrayList<String> tableList, String parentPackageName, String author, DataSourceConfig.Builder DATA_SOURCE_CONFIG, boolean enableTableField) {
        gen(tableList, parentPackageName, author, DATA_SOURCE_CONFIG, enableTableField, "./src/main/java");
    }

    public static void gen(ArrayList<String> tableList, String parentPackageName, String author, DataSourceConfig.Builder DATA_SOURCE_CONFIG, boolean enableTableField, String outPutdDir) {
        try {
            before(DATA_SOURCE_CONFIG);
        } catch (SQLException var7) {
            var7.printStackTrace();
        }

        FastAutoGenerator.create(DATA_SOURCE_CONFIG).globalConfig((builder) -> {
            builder.author(author).disableOpenDir().dateType(DateType.ONLY_DATE).outputDir(outPutdDir);
        }).packageConfig((builder) -> {
            builder.parent(parentPackageName);
        }).strategyConfig((builder) -> {
            if (enableTableField) {
                builder
                        .addInclude(tableList).entityBuilder().enableFileOverride()
                        .addTableFills(new IFill[]{new Column("create_time", FieldFill.INSERT)}).addTableFills(new IFill[]{new Column("create_user_id", FieldFill.INSERT)}).addTableFills(new IFill[]{new Column("create_user_name", FieldFill.INSERT)}).addTableFills(new IFill[]{new Column("update_time", FieldFill.INSERT_UPDATE)}).addTableFills(new IFill[]{new Column("update_user_id", FieldFill.UPDATE)}).addTableFills(new IFill[]{new Column("update_user_name", FieldFill.UPDATE)}).versionColumnName("version").versionColumnName("version").enableLombok().enableChainModel().enableTableFieldAnnotation().mapperBuilder().superClass("com.xiaohuoren.dubbo.mybatis.extmapper.BaseExtMapper");
            } else {
                builder.addInclude(tableList).entityBuilder()
                        .enableFileOverride()
                        .addTableFills(new IFill[]{new Column("create_time", FieldFill.INSERT)}).addTableFills(new IFill[]{new Column("create_user_id", FieldFill.INSERT)}).addTableFills(new IFill[]{new Column("create_user_name", FieldFill.INSERT)}).addTableFills(new IFill[]{new Column("update_time", FieldFill.INSERT_UPDATE)}).addTableFills(new IFill[]{new Column("update_user_id", FieldFill.UPDATE)}).addTableFills(new IFill[]{new Column("update_user_name", FieldFill.UPDATE)}).versionColumnName("version").versionColumnName("version").enableLombok().enableChainModel().mapperBuilder().superClass("com.xiaohuoren.dubbo.mybatis.extmapper.BaseExtMapper");
            }

        }).injectionConfig((builder) -> {
            builder.beforeOutputFile((tableInfo, stringObjectMap) -> {
                if (enableTableField) {
                    String name = tableInfo.getName();
                    String s = JSON.toJSONString(tableInfo);
                    Map map = JSON.parseObject(s);
                    map.put("name", "`" + name + "`");
                    List<TableField> fields = tableInfo.getFields();
                    Iterator var7 = fields.iterator();

                    while (var7.hasNext()) {
                        TableField field = (TableField) var7.next();
                        field.setColumnName("`" + field.getColumnName() + "`");
                    }

                    map.put("fields", fields);
                    stringObjectMap.put("table", map);
                }

            });
        }).templateConfig((builder) -> {
            builder.disable(new TemplateType[]{TemplateType.CONTROLLER, TemplateType.XML, TemplateType.SERVICE, TemplateType.SERVICE_IMPL, TemplateType.MAPPER});
        }).templateEngine(new FreemarkerTemplateEngine()).execute();



        FastAutoGenerator.create(DATA_SOURCE_CONFIG).globalConfig((builder) -> {
            builder.author(author).disableOpenDir().dateType(DateType.ONLY_DATE).outputDir(outPutdDir);
        }).packageConfig((builder) -> {
            builder.parent(parentPackageName);
        }).strategyConfig((builder) -> {
            builder.addInclude(tableList);
        }).templateConfig((builder) -> {
            builder.disable(new TemplateType[]{TemplateType.CONTROLLER, TemplateType.ENTITY, TemplateType.XML});
        }).templateEngine(new FreemarkerTemplateEngine()).execute();
    }
}